Skip to content

Handle replies and disconnects for cancelled commands - #67

Merged
FZambia merged 1 commit into
masterfrom
auto/cancelled-command-futures
Sep 26, 2026
Merged

FZambia merged 1 commit into
masterfrom
auto/cancelled-command-futures

Conversation

@FZambia

@FZambia FZambia commented Sep 11, 2026

Copy link
Copy Markdown
Member

What

Client._future_success and Client._future_error now skip resolving a command future that is already done. They still drop the in-flight entry and cancel its timeout timer.

Why

Cancelling a coroutine that is awaiting a command reply also cancels the future registered for that command. A common way to do that is to wrap an SDK call in asyncio.wait_for(client.rpc(...), timeout) or asyncio.timeout(). Both helpers then set the result on that cancelled future anyway, which raises InvalidStateError:

  • Late reply: when the server's reply to the cancelled command arrives, the exception escapes from _process_reply and kills the task that processes incoming messages. The client still reports CONNECTED, but it stops handling replies, pushes and pings. Every later call times out, until the no-ping timer finally forces a reconnect.
  • Disconnect with the command still in flight: _clear_outgoing_futures raises out of _do_disconnect. disconnect() fails half-way: the state is never set to DISCONNECTED and on_disconnected is never called.

The same applies to connect and subscribe commands (_register_future_with_done), for example when client.connect() itself is cancelled.

Verification

Added tests/test_cancelled_command.py, which uses the in-process fake server:

  • test_late_reply_to_cancelled_command: the fake server holds the reply to an RPC. The test cancels the call with asyncio.wait_for, has the server send the held reply, then asserts that a second RPC still gets its reply and the client stays connected. Before the fix the second RPC failed with OperationTimeoutError, because the message processing task had died with InvalidStateError ("unhandled exception in background task" in the log).
  • test_disconnect_with_cancelled_command_in_flight: cancels an RPC that never gets a reply, then asserts that disconnect() completes, the state is DISCONNECTED and no in-flight commands are left. Before the fix disconnect() raised InvalidStateError from _future_error.

I ran both tests against the unfixed client.py and confirmed they fail as described, then confirmed they pass with the fix. I kept the tests because cancelling SDK calls with a timeout is a normal usage pattern, and nothing else in the suite covers it.

I also ran the full suite (python -m unittest discover -s tests, 125 tests, with the docker-compose Centrifugo running) and ruff check .. Both pass.

Cancelling a coroutine awaiting a command reply (for example
asyncio.wait_for(client.rpc(...), timeout)) cancels the future registered
for that command. _future_success and _future_error resolved that future
unconditionally, which raised InvalidStateError:

- a late reply killed the task processing incoming messages, so no further
  replies, pushes or pings were handled until the no-ping timer forced a
  reconnect;
- a disconnect with such a command still in flight raised from
  _clear_outgoing_futures, aborting disconnect half-way.

Skip resolving futures that are already done, still dropping the in-flight
entry and cancelling its timeout timer.
@FZambia
FZambia merged commit c323d72 into master Sep 26, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant